define([
    'app',
    'css!css/views/property.css'
], function(app) {
    app.controller('PropertyController', ['$filter', '$routeParams', 'Config', 'Dialog', 'Table', 'CategoryService',
        'PropertyService', function($filter, $routeParams, Config, Dialog, Table, CategoryService, PropertyService) {
        var self = this;

        self.imageBaseUrl = Config.imageBaseUrl;
        self.propertyId = $routeParams.id;
        self.propertyName = '';
        self.buyDateFrom = '';
        self.buyDateTo = '';
        self.category = [];
        self.categoryId = '';
        self.properties = [];
        self.showTable = true;
        self.loadPage = 1;
        self.loadSize = 12;
        self.allLoaded = false;

        self.init = function() {
            CategoryService.getSubCategory({}).then(function(resp) {
                self.category = resp.data;
            });
        };

        self.tableParams = Table.$create({
            pageable: true
        }, {
            getPageData: function(params) {
                return PropertyService.getPropertyByPage({
                    categoryId: self.categoryId,
                    id: self.propertyId,
                    name: self.propertyName,
                    buyDateFrom: self.buyDateFrom,
                    buyDateTo: self.buyDateTo,
                    offset: params.offset,
                    limit: params.limit,
                    orderBy: params.orderBy()
                }).then(function(resp) {
                    params.total(resp.data.total);
                    return resp.data.data;
                });
            }
        });

        self.reset = function() {
            self.propertyName = '';
            self.categoryId = '';
            self.buyDateFrom = '';
            self.buyDateTo = '';
        };

        self.search = function(init) {
            self.showResult(self.showTable, init);
        };

        self.edit = function(property) {
            Dialog.$open().editProperty({
                data: (function(param) {
                    if (angular.isObject(param)) {
                        param.buyDate = $filter('date')(param.buyDate, 'yyyy-MM-dd');
                        return param;
                    }
                    return null;
                })(property)
            }).then(function(data) {
                if (data.success) {
                    if (property) {
                        if (self.showTable) {
                            self.search();
                        } else {
                            PropertyService.getProperty({
                                id: property.id
                            }).then(function(resp) {
                                if (resp.success) {
                                    var index = self.properties.indexOf(property);
                                    if (index >= 0 && resp.data.length > 0) {
                                        self.properties[index] = resp.data[0];
                                    }
                                }
                            });
                        }
                    } else {
                        self.search(true);
                    }
                }
            });
        };

        self.remove = function(property) {
            var propertyIds = [];
            if (property) {
                propertyIds.push(property.id);
            } else {
                angular.forEach(self.tableParams.select(), function(item) {
                    propertyIds.push(item.id);
                });
            }
            if (propertyIds.length == 0) {
                Dialog.$alert('请选择需要删除的物品！');
                return;
            }
            
            Dialog.$confirm('确定要删除此物品吗？').then(function() {
                PropertyService.removeProperty(propertyIds).then(function() {
                    if (self.showTable) {
                        self.search();
                    } else {
                        var index = self.propertys.indexOf(property);
                        if (index >= 0) {
                            self.propertys.splice(index, 1);
                        }
                    }
                });
            });
        };

        this.preview = function(property) {
            var template = [
                '<div class="ngdialog-message">',
                '  <div class="message thumbnail">',
                '    <img alt="Loading..." ng-src="' + self.imageBaseUrl + '{{imageUrl}}">',
                '  </div>',
                '</div>'
            ].join('');
            Dialog.$dialog({
                template: template,
                plain: true,
                appendClassName: 'ngdialog-width-500 ngdialog-padding-70',
                controller: ['$scope', function($scope) {
                    $scope.imageUrl = property.imageUrl;
                }]
            });
        };

        self.showResult = function(init, showTable) {
            if (angular.isDefined(showTable)) {
                self.showTable = showTable;
            }
            if (init) {
                self.propertyId = null;
            }
            if (self.showTable) {
                if (init) {
                    self.tableParams.page(1);
                }
                self.tableParams.reload(true);
            } else {
                if (init) {
                    self.allLoaded = false;
                    self.loadPage = 1;
                    self.properties = [];
                }
                self.load();
            }
        }

        self.load = function() {
            PropertyService.getPropertyByPage({
                categoryId: self.categoryId,
                name: self.propertyName,
                buyDateFrom: self.buyDateFrom,
                buyDateTo: self.buyDateTo,
                offset: (self.loadPage - 1) * self.loadSize,
                limit: self.loadSize
            }).then(function(resp) {
                self.loadPage++;
                var data = resp.data.data;
                if (data != null && data.length > 0) {
                    angular.forEach(data, function(temp) {
                        self.properties.push(temp);
                    });
                }
                if (data == null || data.length < self.loadSize) {
                    self.allLoaded = true;
                }
            });
        };
    }]);
});
